package com.aracky.flowable.config;


import com.aracky.common.config.MyMetaObjectHandler;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zengzhiping
 * @date 2022/07/19 16:36
 **/

@Configuration
@EnableSwagger2 // 这个注解无论什么时候都需要
@EnableKnife4j
public class Swagger3Config implements ApplicationListener<WebServerInitializedEvent> {

    private Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Bean
    public Docket createRestApi() {
        List<RequestParameter> pars = new ArrayList<>();
        RequestParameterBuilder tokenPar = new RequestParameterBuilder();
        tokenPar.name("Authorization").description("令牌").in(ParameterType.HEADER).required(true);
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .groupName("aracky-flowable")
                .globalRequestParameters(globalRequestParameters())
                //.globalRequestParameters(pars)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("aracky User API Ver1.0")
                .description("aracky @2022")
                .license("")
                .licenseUrl("https://oa.in-aracky.com")
                .termsOfServiceUrl("")
                .version("1.0")
                .contact(new Contact("流程系统", "", ""))
                .build();
    }

    private List<RequestParameter> globalRequestParameters() {
        List<RequestParameter> requestParameters = new ArrayList<>();
        RequestParameterBuilder builder = new RequestParameterBuilder();
        requestParameters.add(
                builder.name("Authorization")
                        .required(false)
                        .in(ParameterType.HEADER)
                        .build()
        );
        return requestParameters;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            //获取IP
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            //获取端口号
            int port = event.getWebServer().getPort();
            //获取应用名
            String applicationName = event.getApplicationContext().getApplicationName();
            logger.info("项目启动成功！接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/doc.html");
        } catch (java.net.UnknownHostException e) {
            logger.error("启动异常:", e);
        }
    }
}
